package com.iatinspur.dubbo.test;

import com.iatinspur.dubbo.factory.GenericServiceFactory;
import com.iatinspur.dubbo.service.ProviderStreamService;
import org.apache.dubbo.common.stream.StreamObserver;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
import java.util.concurrent.CountDownLatch;


/**
 * dubbo泛化版的流式调用 还想不是很支持
 */
public class TripleStreamDoProvider implements Serializable {

    /**
     * 走dubbo协议会发生阻塞
     */
    @Test
    public  void doDubbo() {
        final CountDownLatch latch = new CountDownLatch(1);
        ProviderStreamService service = GenericServiceFactory.getDubboService(ProviderStreamService.class,"1.0.0","tri",60914);
        service.dubboServerStream("黄剑", new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.err.println(data);
            }

            @Override
            public void onError(Throwable throwable) {

            }
            @Override
            public void onCompleted() {
                System.err.println("完毕");
                latch.countDown();
            }
        });
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("1111");
    }

    /**
     *  走nacos没有问题
     *  这是个异步调用
     *  【所以】 要么加锁锁住主线程 如   final CountDownLatch latch = new CountDownLatch(1);  +   latch.await();
     *  【或者】 GenericServiceFactory.getNacosService 方法中 reference.setAsync(true)
     */
    @Test
    public  void doNacos() {
        final CountDownLatch latch = new CountDownLatch(1);
        ProviderStreamService service = GenericServiceFactory.getNacosService(ProviderStreamService.class,"1.0.0","tri");
        service.dubboServerStream("黄剑", new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.err.println(data);
            }

            @Override
            public void onError(Throwable throwable) {

            }
            @Override
            public void onCompleted() {
                System.err.println("完毕");
                latch.countDown();
            }
        });
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("1111");
    }


    /**
     *   走zookeeper没有问题
     *  这是个异步调用
     *  【所以】 要么加锁锁住主线程 如   final CountDownLatch latch = new CountDownLatch(1);  +   latch.await();
     *  【或者】 GenericServiceFactory.getNacosService 方法中 reference.setAsync(true)
     */
    @Test
    public  void doZookeeper() {
        final CountDownLatch latch = new CountDownLatch(1);
        ProviderStreamService service = GenericServiceFactory.getZookeeperService(ProviderStreamService.class,"1.0.0","tri");
        service.dubboServerStream("黄剑", new StreamObserver<String>() {
            @Override
            public void onNext(String data) {
                System.err.println(data);
            }
            @Override
            public void onError(Throwable throwable) {

            }
            @Override
            public void onCompleted() {
                System.err.println("完毕");
                latch.countDown();
            }
        });
        try {
            latch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("1111");
    }



}
